package com.yxwl.demo.ui.print.printer;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.print.base.bean.DeviceItem;
import com.print.base.bean.PrinterConstantPool;
import com.print.base.bean.TaskCallBean;
import com.print.base.listen.ConnectListener;
import com.print.base.listen.TaskCallback;
import com.print.printer.Printer;
import com.print.printer.PrinterManage;
import com.yxwl.demo.ui.print.PrintActivity;
import com.yxwl.demo.utils.OnCallback;
import com.yxwl.demo.widget.CustomProgress;
import com.yxwl.demo.widget.LogView;

import java.util.ArrayList;
import java.util.List;

public class PrinterUtil {
    public Printer printer;
    private DeviceItem item;
    private final List<ConnectListener> connects = new ArrayList<>();
    private final List<TaskCallback> taskCalls = new ArrayList<>();
    private PrintImgUtil printUtil;
    public int type = PrinterConstantPool.SocketType.SPP;
    static volatile PrinterUtil instance;

    public static PrinterUtil getInstance() {
        if (instance == null) {
            synchronized (PrinterUtil.class) {
                if (instance == null) {
                    instance = new PrinterUtil();
                }
            }
        }
        return instance;
    }

    public PrinterUtil() {
        if (type !=PrinterConstantPool.SocketType.USB){
            printer = PrinterManage.getInstance().getPrinter(PrinterConstantPool.SocketType.SPP);
        }
    }

    /**
     * 更新打印机信息
     */
    public void setDevice(DeviceItem device, @NonNull OnCallback<Object> callback) {
        if (device == null || device.address == null) {
            return;
        }
        PrinterUtil printer = PrinterUtil.getInstance();
        if (printer.item == null || printer.item.address.equals(device.address)) {
            printer.item = device;
            callback.onCallback(null);
        } else {
            printer.item = device;
            printer.disconnect();
            callback.onCallback(null);
        }
    }


    public DeviceItem getItem() {
        return item;
    }


    public void initPrintUtil(String name) {
//        if (name.startsWith(PrinterConstantPool.Model.YP11)
//                || name.startsWith(PrinterConstantPool.Model.L12)) {
////            printUtil = new PrintImg_ESC(printer);
//        } else if (name.startsWith(PrinterConstantPool.Model.Y468)
//                || name.startsWith(PrinterConstantPool.Model.Y810BT)
//                || name.startsWith(PrinterConstantPool.Model.A318BT)
//                || name.startsWith(PrinterConstantPool.Model.Y318BT)
//                || name.startsWith(PrinterConstantPool.Model.Q318ABT)
//                || name.startsWith(PrinterConstantPool.Model.QR380A)
//                || name.startsWith(PrinterConstantPool.Model.QR_380A)
//                || name.startsWith(PrinterConstantPool.Model.QR_386A)
//        ) {
////            printUtil = new TSPLUtil(printer);
//        } else if (name.startsWith(PrinterConstantPool.Model.A80)
//                || name.startsWith(PrinterConstantPool.Model.C80)
//                || name.startsWith(PrinterConstantPool.Model.D810)
//        ) {
//            printUtil = new A80Util(printer);
//        }
        printUtil = new A80Util(printer);
        printer.setListener(connectListener);//设置连接监听
        printer.setTaskCallback(callback);//设置全局通讯回调
    }



    public void connect() {
        if (item == null) {
            return;
        }
        initPrintUtil(item.name);
        if (printUtil == null) {
            return;
        }
        printer.connect(item);
    }

    public void disconnect() {
        printer.disconnect();
    }

    public boolean isConnect() {
        return printer.isConnect();
    }

    public void release() {
        item = null;
        printer.release();
    }



    public void print(Activity activity) {
        printUtil.print(activity);
    }


    public void addConnectListener(ConnectListener listener) {
        if (listener != null) {
            connects.add(listener);
        }
    }

    public void removeConnectListener(ConnectListener listener) {
        if (listener != null) {
            connects.remove(listener);
        }
    }

    public void addTaskCall(TaskCallback taskCall) {
        if (taskCall != null) {
            taskCalls.add(taskCall);
        }
    }

    public void removeTaskCall(TaskCallback taskCall) {
        if (taskCall != null) {
            taskCalls.remove(taskCall);
        }
    }

    private ConnectListener connectListener = new ConnectListener() {
        @Override
        public void onConneted() {
            for (ConnectListener connect : connects) {
                connect.onConneted();
            }
            CustomProgress.dismissPD();
            LogView.log("连接成功");
            ivHide(false);
        }

        @Override
        public void onConnetFailed(String msg) {
            for (ConnectListener connect : connects) {
                connect.onConnetFailed(msg);
            }
            ivHide(true);
            CustomProgress.dismissPD();
            LogView.log("连接失败: " + msg);
        }

        @Override
        public void closed() {
            for (ConnectListener connect : connects) {
                connect.closed();
            }
            ivHide(true);
            CustomProgress.dismissPD();
            LogView.log("连接已断开");
        }
    };

    private TaskCallback callback = new TaskCallback() {
        @Override
        public void sendStatus(TaskCallBean bean) {
            for (TaskCallback call : taskCalls) {
                call.sendStatus(bean);
            }
        }

        @Override
        public void readCall(TaskCallBean bean) {
            for (TaskCallback call : taskCalls) {
                call.readCall(bean);
            }
        }

        @Override
        public int timeOut() {
            return 3000;
        }
    };

    public static void ivHide(boolean hide) {
        ImageView iv = PrintActivity.icWR.get();
        if (iv!=null){
            iv.setVisibility(hide? View.GONE:View.VISIBLE);
        }
    }
}
